/*
 * @Author: Wilbur
 * @Date: 2019-01-18 10:10:29
 * @Last Modified by: Wilbur
 * @Last Modified time: 2019-01-18 16:27:14
 */

<template>
    <div class="container finance-list-container">
        <div class="content-wrapper">
            <el-row
                class="search-wrapper"
                :gutter="20"
            >
                <el-form
                    label-position="right"
                    label-width="84px"
                    ref="searchfrom"
                    size="small">
                    <el-form-item label="用户类型">
                        <el-select
                            v-model="filterData.aim_user_type"
                            filterable
                            @change="getTableData()"
                            placeholder="请选择">
                            <el-option
                                label="不限"
                                :value="0"/>
                            <el-option
                                label="广告主"
                                :value="1"/>
                            <el-option
                                label="校园媒体"
                                :value="2"/>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="账单类型">
                        <el-select
                            v-model="filterData.type"
                            filterable
                            @change="getTableData()"
                            placeholder="请选择"
                        >
                            <el-option
                                label="不限"
                                :value="0"/>
                            <el-option
                                label="提现"
                                :value="2"/>
                            <el-option
                                label="用户充值"
                                :value="3"/>
                            <el-option
                                label="系统赠送"
                                :value="4"/>
                            <el-option
                                label="系统扣除"
                                :value="6"/>
                            <el-option
                                label="系统赠送(现金)"
                                :value="8"/>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="收入/支出">
                        <el-select
                            v-model="filterData.money_type"
                            @change="getTableData()"
                            placeholder="请选择"
                        >
                            <el-option
                                label="不限"
                                :value="0"/>
                            <el-option
                                label="收入"
                                :value="1"/>
                            <el-option
                                label="支出"
                                :value="2"/>
                            <el-option
                                label="冻结"
                                :value="101"/>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="状态">
                        <el-select
                            v-model="filterData.status"
                            filterable
                            @change="getTableData()"
                            placeholder="请选择"
                        >
                            <el-option
                                label="不限"
                                :value="0"/>
                            <el-option
                                label="未开始"
                                :value="-1"/>
                            <el-option
                                label="进行中"
                                :value="1"/>
                            <el-option
                                label="终止或失败"
                                :value="2"/>
                            <el-option
                                label="完成"
                                :value="3"/>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="合同编号">
                        <el-input
                            v-model.trim="filterData.contract_no"
                            placeholder="请输入合同编号"
                            type="text"
                            size="small"
                            style="width: 193px;"
                            @keyup.native.enter="getTableData()"
                        />
                    </el-form-item>

                    <el-form-item
                        label="手机号"
                    >
                        <el-input
                            v-model="filterData.aim_user_phone"
                            placeholder="请输入内容"
                            suffix-icon="el-icon-search"
                            size="small"
                            clearable
                            @keyup.native.enter="getTableData(1)"
                            @change="getTableData(1)"
                        />
                    </el-form-item>

                    <el-form-item label="开始时间">
                        <el-date-picker
                            v-model="filterData.start_time"
                            value-format="timestamp"
                            type="datetime"
                            @change="getTableData()"
                            placeholder="选择开始时间"
                            style="width: 193px;"/>
                    </el-form-item>

                    <el-form-item label="结束时间">
                        <el-date-picker
                            type="datetime"
                            placeholder="选择结束时间"
                            value-format="timestamp"
                            v-model="filterData.end_time"
                            @change="getTableData()"
                            style="width: 193px;"/>
                    </el-form-item>
                    <el-button
                        size="small"
                        style="margin-left: 18px;"
                        @click="fnResetFrom('searchfrom')"
                    >重置筛选</el-button>
                    <el-button
                        size="small"
                        type="primary"
                        style="margin-left: 18px;"
                        @click="getTableData()"
                    >搜索</el-button>
                    <el-button
                        size="small"
                        type="primary"
                        @click="fnExportData()"
                    >导出搜索结果</el-button>
                </el-form>
            </el-row>
            <div class="table-wrapper">
                <el-table
                    :data="tableData"
                    height="100%"
                    style="width: 100%;"
                >
                    <el-table-column
                        fixed="left"
                        header-align="left"
                        :width="50"
                        label="序号"
                    >
                        <template slot-scope="scope">
                            <div class="table-index">
                                {{ scope.$index + 1 }}
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        fixed="left"
                        prop="order_no"
                        :width="200"
                        label="账单编号"
                    />
                    <el-table-column
                        header-align="left"
                        label="用户类型"
                    >
                        <template slot-scope="scope">
                            <template v-if="scope.row.aim_user_type == 1">广告主</template>
                            <template v-if="scope.row.aim_user_type == 2">校园媒体</template>
                            <template v-if="scope.row.aim_user_type == 4">代理</template>
                        </template>
                    </el-table-column>

                    <el-table-column
                        prop="aim_user.phone"
                        :width="150"
                        label="手机号">
                        <template slot-scope="scope">
                            <a
                                href="javascript:;"
                                @click="handleRoute('supplyList', {phone: scope.row.aim_user.phone})"
                                style="color: #409EFF;">{{ scope.row.aim_user.phone }}</a>
                        </template>
                    </el-table-column>

                    <el-table-column
                        prop="aim_user.nickname"
                        :width="150"
                        :formatter="emptyFormatter"
                        show-overflow-tooltip
                        label="用户名称"
                    />
                    <el-table-column
                        prop="resource_no"
                        label="账单类型">
                        <template slot-scope="scope">
                            <template v-if="scope.row.type == 2">提现</template>
                            <template v-if="scope.row.type == 3">用户充值</template>
                            <template v-if="scope.row.type == 4">系统赠送</template>
                            <template v-if="scope.row.type == 6">系统扣除</template>
                            <template v-if="scope.row.type == 8">系统赠送(现金)</template>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="title"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        label="标题"
                    />
                    <el-table-column
                        prop="money_type"
                        :width="90"
                        label="收入/支出">
                        <template slot-scope="scope">
                            <template v-if="scope.row.money_type == 1">收入</template>
                            <template v-if="scope.row.money_type == 2">支出</template>
                            <template v-if="scope.row.typmoney_typee == 101">冻结</template>
                        </template>
                    </el-table-column>

                    <el-table-column
                        prop="money"
                        label="金额"
                    />
                    <el-table-column
                        prop="remark"
                        label="账单备注"
                        :min-width="200"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="complete_remark"
                        label="完成备注"
                        :min-width="200"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="contract_no"
                        label="合同编号"
                        :min-width="150"
                        :formatter="emptyFormatter"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        prop="status"
                        show-overflow-tooltip
                        :min-width="100"
                        label="状态"
                    >
                        <template slot-scope="scope">
                            <template v-if="scope.row.status == 0">未开始</template>
                            <template v-if="scope.row.status == 1">进行中</template>
                            <template v-if="scope.row.status == 2">终止或失败</template>
                            <template v-if="scope.row.status == 3">完成</template>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="create_time"
                        :width="150"
                        label="创建时间">
                        <template slot-scope="scope">
                            {{ scope.row.create_time*1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                        </template>
                    </el-table-column>

                    <el-table-column
                        prop="date"
                        label="操作"
                        fixed="right"
                        :width="180"
                    >
                        <template slot-scope="scope">
                            <div v-if="scope.row.status == 1 && scope.row.type == 2">
                                <el-button
                                    type="text"
                                    @click="fnSetOrderStatus(scope.row.order_no, 1)"
                                >完成</el-button>
                                <el-button
                                    type="text"
                                    @click="fnSetOrderStatus(scope.row.order_no, 2)"
                                >自动打款</el-button>
                                <el-button
                                    type="text"
                                    @click="fnSetOrderStatus(scope.row.order_no, 3)"
                                >拒绝</el-button>
                            </div>
                            <div v-else>--</div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="pagination-wrapper clearfix">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="page.now_page"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData"
                />
            </div>
        </div>
    </div>
</template>

<script>
export default {
    components: {},
    data() {
        return {
            authResult: {},
            tableData: [],
            filterData: {
                aim_user_type: 0,
                type: 0,
                money_type: 0,
                status: '',
                start_time: '',
                end_time: '',
                contract_no: '',
                aim_user_phone: ''
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            currTime: 0,
        };
    },
    created() {
        this.authResult = this.$route.meta.authResult;
        this.$store.commit('SET_HEADER', [
            {
                title: "广告系统",
            },
            {
                title: '资源列表'
            }
        ]);
        this.filterData.aim_user_type = this.$route.query.aim_user_type ? eval(this.$route.query.aim_user_type) : 0;
        this.filterData.type = this.$route.query.type ? eval(this.$route.query.type) : 0;
        this.filterData.money_type = this.$route.query.money_type ? eval(this.$route.query.money_type) : 0;
        this.filterData.status = this.$route.query.status ? eval(this.$route.query.status) : 0;
        this.filterData.start_time = this.$route.query.start_time ? this.$route.query.start_time : '';
        this.filterData.end_time = this.$route.query.end_time ? this.$route.query.end_time : '';
        this.filterData.aim_user_phone = this.$route.query.phone ? this.$route.query.phone : '';
        this.fnGetQuery();
        // this.filterData.contract_no = this.$route.query.contract_no ? this.$route.query.contract_no : '';
        this.getTableData(this.$route.query.page);

    },
    filters: {
        emptyFilter(val){
            if (!val) {
                return '--';
            }
            return val;
        }
    },
    methods: {
        /**
         * 查看赠送记录
         */
        fnGetQuery () {
            const supplay_no = this.$route.query;

            if (supplay_no['supply_no'] !== undefined || supplay_no['supply_no'] !== 'undefined') {
                this.filterData.contract_no = supplay_no.supply_no;
            }
        },

        fnSetOrderStatus(order_no, type) {
            let data = {};

            data.type = type;
            data.order_no = order_no;
            //完成
            if(type == 1) {
                this.$prompt('请输入完成后备注', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                }).then(({ value }) => {
                    data.complete_remark = value;
                    this.fnSetOrder(data);
                }).catch(() => {
                });
            }else if(type == 3) {
                //拒绝
                this.$prompt('请输入拒绝原因', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    inputPattern: /\S/,
                    inputErrorMessage: '请输入拒绝原因'
                }).then(({ value }) => {
                    data.refuse_reason = value;
                    this.fnSetOrder(data);
                }).catch(() => {
                });
            }else{
                //自动打款
                this.fnSetOrder(data);
            }
        },
        fnSetOrder(data) {
            this.$post('/wei4/finance%5Cfinance_system_order%5Cset_status', data, resp => {

                if (resp.code == 1) {
                    this.getTableData();
                    this.$message({
                        type: 'success',
                        message: '操作成功',
                        center: true
                    });
                } else {
                    this.$message({
                        type: 'warning',
                        message: resp.msg,
                        center: true
                    });
                }
            });
        },
        fnExportData() {
            this.$confirm('确定导出数据？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let data = this.filterData;
                let ticket = localStorage.getItem('sessionTicket');
                let base = this.$baseUrl;

                window.open(`${base}/wei4/finance%5Cfinance_system_order%5Cexport_list?aim_user_type=${data.aim_user_type}&type=${data.type}&money_type=${data.money_type}&status=${data.status}&start_time&end_time&end_time&contract_no&__ticket=${ticket}`);
            }).catch(() => {
                console.log('cancel');
            });
        },
        fnResetFrom(formName) {
            this.filterData = {
                aim_user_type: 0,
                type: 0,
                money_type: 0,
                status: '',
                start_time: '',
                end_time: '',
                contract_no: '',
                aim_user_phone: ''
            };
            this.getTableData();
        },
        /**
       * 获取表格数据
       */
        getTableData(page) {
            let time = new Date().getTime();

            this.currTime = time;

            this.$router.replace({
                query: {
                    ...this.filterData,
                    page: page || this.page.now_page
                }
            });
            this.$post('/wei4/finance%5Cfinance_system_order%5Cget_list', {
                ...this.filterData,
                page_id: page || this.page.now_page
            }, resp => {
                if(this.currTime != time){
                    return ;
                }
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                    if (this.page.now_page > resp.data.page.total_page && this.page.now_page !== 1) {
                        this.getTableData(resp.data.page.total_page);
                    }
                } else {
                    this.$message({
                        type: 'warning',
                        message: resp.msg,
                        center: true
                    });
                }
            });
        },

        /**
       * 跳转页面
       */
        handleRoute(name, query) {
            let obj = {
                name
            };

            if (query) {
                obj.query = query;
            }

            this.$router.push(obj);
        },

        /**
       * 列表渲染辅助函数：列表项为空处理
       */
        emptyFormatter(row, column, val) {
            if (!val) {
                return '--';
            }
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
  .container {
    display: flex;
    flex-direction: column;
    width: 100%;
  }
  .operation-wrapper {
    display: flex;
    flex-direction: row-reverse;
    padding: 12px 23px;
    background-color: #fff;

    .el-button {
      margin-left: 10px;
    }
  }
  .content-wrapper {
    flex: 1;
    display: flex;
    flex-direction: column;
    margin: 15px;
    overflow: hidden;
  }
  .search-wrapper {
    padding: 15px;
    background-color: #fff;
  }
  .filter-item {
    .el-form-item {
      white-space: nowrap;
    }
    .el-input {
      width: auto;
      vertical-align: middle;
    }
    .el-button {
      margin-left: 15px;
      vertical-align: middle;
    }
  }
  .table-wrapper {
    flex: 1;
    background-color: #fff;

    .el-button {
      padding: 0;
    }
    .icon-bianji {
      font-size: 14px;
      color: #b9b9b9;
      cursor: pointer;
    }
    .btn-delete {
      color: #fd4d32;
      &.is-disabled {
        color: #c0c4cc;
        cursor: not-allowed;
      }
    }
    .submit-config-item {
      display: inline-block;
      position: relative;
      padding-right: 10px;
      font-size: 14px;

      & + .submit-config-item {
        padding-left: 10px;
        &::before {
          content: "";
          position: absolute;
          top: 50%;
          left: 0;
          width: 1px;
          height: 14px;
          transform: translate(0, -50%);
          background-color: #d8d8d8;
        }
      }

      .num {
        vertical-align: middle;
      }
      .iconfont {
        font-size: 16px;
        color: #b9b9b9;
        vertical-align: middle;
      }

      .icon-tupian {
        font-size: 20px;
      }
    }
  }
  .pagination-wrapper {
    padding-top: 10px;
    .el-pagination {
      float: right;
      padding: 0;
      background-color: transparent;
    }
  }
  .dialog-link {
    & + .dialog-link {
      margin-top: 20px;
    }
    .link-label {
      font-weight: 500;
      margin-right: 20px;
    }
  }
</style>
<style lang="less">
.finance-list-container{
    .search-wrapper{
        .el-form-item{
            display: inline-block;
        }
    }
}
</style>
